package com.erin724.erinsleep;

import java.util.Calendar;
import java.util.GregorianCalendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
	public final static String HOUR = "com.erin724.erinsleep.HOUR";
	public final static String MINUTE = "com.erin724.erinsleep.MINUTE";
	public final static String REQCODE = "com.erin724.erinsleep.REQCODE";
	static final int CORE1_SLEEPTIME_PRESS = 0;
	static final int CORE1_WAKETIME_PRESS = 1;
	static final int NAP1_SLEEPTIME_PRESS = 2;
	static final int NAP1_WAKETIME_PRESS = 3;
	static final int NAP2_SLEEPTIME_PRESS = 4;
	static final int NAP2_WAKETIME_PRESS = 5;
	static final int NAP3_SLEEPTIME_PRESS = 6;
	static final int NAP3_WAKETIME_PRESS = 7;
	/*These Buttons contain the text that updateCalendars() uses to set
	 * the respective calendar objects to the correct times
	 */
	Button Core1_SleepTime;
	Button Nap1_SleepTime;
	Button Nap2_SleepTime;
	Button Nap3_SleepTime;
	Button Core1_WakeTime;
	Button Nap1_WakeTime;
	Button Nap2_WakeTime;
	Button Nap3_WakeTime;
	/*****/
	TextView Core1_Duration;
	TextView Nap1_Duration;
	TextView Nap2_Duration;
	TextView Nap3_Duration;
	GregorianCalendar CurrentTime;
	/*These Calendar objects are the final arbiter of alarm times.
	 * Updating them, then calling updateAlarms() will set the alarms.
	 * Normally, we should use updateCalendars() to grab the values
	 * from the user interface.
	 */
	GregorianCalendar Core1_SleepCal;
	GregorianCalendar Core1_WakeCal;
	GregorianCalendar Nap1_SleepCal;
	GregorianCalendar Nap1_WakeCal;
	GregorianCalendar Nap2_SleepCal;
	GregorianCalendar Nap2_WakeCal;
	GregorianCalendar Nap3_SleepCal;
	GregorianCalendar Nap3_WakeCal;
	/*****/
	PendingIntent pi;
	AlarmManager am;
		

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Core1_SleepTime = (Button) findViewById(R.id.Core1_Sleeptime);
		Core1_WakeTime = (Button) findViewById(R.id.Core1_Waketime);
		Core1_Duration = (TextView) findViewById(R.id.Core1_Duration);
		Nap1_SleepTime = (Button) findViewById(R.id.Nap1_Sleeptime);
		Nap1_WakeTime = (Button) findViewById(R.id.Nap1_Waketime);
		Nap1_Duration = (TextView) findViewById(R.id.Nap1_Duration);
		Nap2_SleepTime = (Button) findViewById(R.id.Nap2_Sleeptime);
		Nap2_WakeTime = (Button) findViewById(R.id.Nap2_Waketime);
		Nap2_Duration = (TextView) findViewById(R.id.Nap2_Duration);
		Nap3_SleepTime = (Button) findViewById(R.id.Nap3_Sleeptime);
		Nap3_WakeTime = (Button) findViewById(R.id.Nap3_Waketime);
		Nap3_Duration = (TextView) findViewById(R.id.Nap3_Duration);
		am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
		CurrentTime = new GregorianCalendar();
		Core1_SleepCal = new GregorianCalendar();
		Core1_WakeCal = new GregorianCalendar();
		Nap1_SleepCal = new GregorianCalendar();
		Nap1_WakeCal = new GregorianCalendar();
		Nap2_SleepCal = new GregorianCalendar();
		Nap2_WakeCal = new GregorianCalendar();
		Nap3_SleepCal = new GregorianCalendar();
		Nap3_WakeCal = new GregorianCalendar();
		updateCalendars();
		updateAlarms();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	/*This will signal the user is going to sleep and reset the next alarm for the correct duration
	 * 
	 */
	public void goToSleep(View v) {
		//setup all my calendar times
		updateCalendars();
		//Figure out which nap the user is about to take
		GregorianCalendar nextWake = new GregorianCalendar();
		int nextNap = CORE1_WAKETIME_PRESS;
		long duration = Long.parseLong(Core1_Duration.getText().toString().split(" ")[0]);
		nextWake.setTime(Core1_WakeCal.getTime());
		if(Nap1_WakeCal.before(nextWake)) {
			nextNap = NAP1_WAKETIME_PRESS;
			nextWake.setTime(Nap1_WakeCal.getTime());
			duration = Long.parseLong(Nap1_Duration.getText().toString().split(" ")[0]);
		}
		if(Nap2_WakeCal.before(nextWake)) {
			nextNap = NAP2_WAKETIME_PRESS;
			nextWake.setTime(Nap2_WakeCal.getTime());
			duration = Long.parseLong(Nap2_Duration.getText().toString().split(" ")[0]);
		}
		if(Nap3_WakeCal.before(nextWake)) {
			nextNap = NAP3_WAKETIME_PRESS;
			nextWake.setTime(Nap3_WakeCal.getTime());
			duration = Long.parseLong(Nap3_Duration.getText().toString().split(" ")[0]);
		}
		//convert duration from minutes to millis
		duration = duration * 60000;
		//reset the next alarm for the correct time using the duration amounts of each nap
		if(nextNap == CORE1_WAKETIME_PRESS) {
			nextWake.setTimeInMillis(CurrentTime.getTimeInMillis() + duration);
			Core1_WakeCal.setTime(nextWake.getTime());
			Core1_SleepCal.add(Calendar.DATE, 1);
		}
		if(nextNap == NAP1_WAKETIME_PRESS) {
			nextWake.setTimeInMillis(CurrentTime.getTimeInMillis() + duration);
			Nap1_WakeCal.setTime(nextWake.getTime());
			Nap1_SleepCal.add(Calendar.DATE, 1);
		}
		if(nextNap == NAP2_WAKETIME_PRESS) {
			nextWake.setTimeInMillis(CurrentTime.getTimeInMillis() + duration);
			Nap2_WakeCal.setTime(nextWake.getTime());
			Nap2_SleepCal.add(Calendar.DATE, 1);
		}
		if(nextNap == NAP3_WAKETIME_PRESS) {
			nextWake.setTimeInMillis(CurrentTime.getTimeInMillis() + duration);
			Nap3_WakeCal.setTime(nextWake.getTime());
			Nap3_SleepCal.add(Calendar.DATE, 1);
		}
		//alarm reset
		updateAlarms();
	}
	
	//This will open our set_time activity, which sets the time of one user chosen alarm
	//This will call the activity_set_time activity to set the time of a button that is pressed
	public void setTime(View v) {
		Button b = (Button) v;
		String buttonText = b.getText().toString();
		String hour = buttonText.split(":")[0];
		String minute = buttonText.split(":")[1];
		Intent intent = new Intent(this, SetTimeActivity.class);
		intent.putExtra(MINUTE, Integer.parseInt(minute));
		intent.putExtra(HOUR, Integer.parseInt(hour));
		if(Core1_SleepTime.getId() == b.getId()) {
			intent.putExtra(REQCODE, CORE1_SLEEPTIME_PRESS);
			startActivityForResult(intent, CORE1_SLEEPTIME_PRESS);
		}
		else if(Core1_WakeTime.getId() == b.getId()) {
			intent.putExtra(REQCODE, CORE1_WAKETIME_PRESS);
			startActivityForResult(intent, CORE1_WAKETIME_PRESS);
		}
		else if(Nap1_SleepTime.getId() == b.getId()) {
			intent.putExtra(REQCODE, NAP1_SLEEPTIME_PRESS);
			startActivityForResult(intent, NAP1_SLEEPTIME_PRESS);
		}
		else if(Nap1_WakeTime.getId() == b.getId()) {
			intent.putExtra(REQCODE, NAP1_WAKETIME_PRESS);
			startActivityForResult(intent, NAP1_WAKETIME_PRESS);
		}
		else if(Nap2_SleepTime.getId() == b.getId()) {
			intent.putExtra(REQCODE, NAP2_SLEEPTIME_PRESS);
			startActivityForResult(intent, NAP2_SLEEPTIME_PRESS);
		}
		else if(Nap2_WakeTime.getId() == b.getId()) {
			intent.putExtra(REQCODE, NAP2_WAKETIME_PRESS);
			startActivityForResult(intent, NAP2_WAKETIME_PRESS);
		}
		else if(Nap3_SleepTime.getId() == b.getId()) {
			intent.putExtra(REQCODE, NAP3_SLEEPTIME_PRESS);
			startActivityForResult(intent, NAP3_SLEEPTIME_PRESS);
		}
		else if(Nap3_WakeTime.getId() == b.getId()) {
			intent.putExtra(REQCODE, NAP3_WAKETIME_PRESS);
			startActivityForResult(intent, NAP3_WAKETIME_PRESS);
		}
	}
	
	
	/*This will take our user input of a sleep or wake time, update the UI with the new value
	 * and update all calendar objects and alarms.
	 */
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		String hour = Integer.toString(data.getIntExtra(HOUR, 0));
		String minute = Integer.toString(data.getIntExtra(MINUTE, 0));
		String time = hour;
		time += ":";
		if(Integer.parseInt(minute)<10){
			time += "0";
		}
		time += minute;
		if(requestCode == CORE1_SLEEPTIME_PRESS) {
			Core1_SleepTime.setText(time);
		}
		else if (requestCode == CORE1_WAKETIME_PRESS) {
			Core1_WakeTime.setText(time);
		}
		else if (requestCode == NAP1_SLEEPTIME_PRESS) {
			Nap1_SleepTime.setText(time);
		}
		else if (requestCode == NAP1_WAKETIME_PRESS) {
			Nap1_WakeTime.setText(time);
		}
		else if (requestCode == NAP2_SLEEPTIME_PRESS) {
			Nap2_SleepTime.setText(time);
		}
		else if (requestCode == NAP2_WAKETIME_PRESS) {
			Nap2_WakeTime.setText(time);
		}
		else if (requestCode == NAP3_SLEEPTIME_PRESS) {
			Nap3_SleepTime.setText(time);
		}
		else if (requestCode == NAP3_WAKETIME_PRESS) {
			Nap3_WakeTime.setText(time);
		}
		updateCalendars();
		updateAlarms();
	}
	
	
	//This will update all of the alarms based on the settings of our calendar objects
	void updateAlarms() {
		Intent alarmintent = new Intent(this, AlarmActivity.class);
			
		//set the alarms
		pi = PendingIntent.getActivity(this, CORE1_SLEEPTIME_PRESS, alarmintent, PendingIntent.FLAG_UPDATE_CURRENT);
		am.setRepeating(AlarmManager.RTC_WAKEUP, Core1_SleepCal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);
		pi = PendingIntent.getActivity(this, CORE1_WAKETIME_PRESS, alarmintent, PendingIntent.FLAG_UPDATE_CURRENT);
		am.setRepeating(AlarmManager.RTC_WAKEUP, Core1_WakeCal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);
		pi = PendingIntent.getActivity(this, NAP1_SLEEPTIME_PRESS, alarmintent, PendingIntent.FLAG_UPDATE_CURRENT);
		am.setRepeating(AlarmManager.RTC_WAKEUP, Nap1_SleepCal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);
		pi = PendingIntent.getActivity(this, NAP1_WAKETIME_PRESS, alarmintent, PendingIntent.FLAG_UPDATE_CURRENT);
		am.setRepeating(AlarmManager.RTC_WAKEUP, Nap1_WakeCal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);
		pi = PendingIntent.getActivity(this, NAP2_SLEEPTIME_PRESS, alarmintent, PendingIntent.FLAG_UPDATE_CURRENT);
		am.setRepeating(AlarmManager.RTC_WAKEUP, Nap2_SleepCal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);
		pi = PendingIntent.getActivity(this, NAP2_WAKETIME_PRESS, alarmintent, PendingIntent.FLAG_UPDATE_CURRENT);
		am.setRepeating(AlarmManager.RTC_WAKEUP, Nap2_WakeCal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);
		pi = PendingIntent.getActivity(this, NAP3_SLEEPTIME_PRESS, alarmintent, PendingIntent.FLAG_UPDATE_CURRENT);
		am.setRepeating(AlarmManager.RTC_WAKEUP, Nap3_SleepCal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);
		pi = PendingIntent.getActivity(this, NAP3_WAKETIME_PRESS, alarmintent, PendingIntent.FLAG_UPDATE_CURRENT);
		am.setRepeating(AlarmManager.RTC_WAKEUP, Nap3_WakeCal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);
	}
	
	
	//This will update all of our calendar objects based on the input values of our user interface
	void updateCalendars() {
		CurrentTime.setTimeInMillis(System.currentTimeMillis());
		Core1_SleepCal.setTimeInMillis(System.currentTimeMillis());
		Core1_WakeCal.setTimeInMillis(System.currentTimeMillis());
		Nap1_SleepCal.setTimeInMillis(System.currentTimeMillis());
		Nap1_WakeCal.setTimeInMillis(System.currentTimeMillis());
		Nap2_SleepCal.setTimeInMillis(System.currentTimeMillis());
		Nap2_WakeCal.setTimeInMillis(System.currentTimeMillis());
		Nap3_SleepCal.setTimeInMillis(System.currentTimeMillis());
		Nap3_WakeCal.setTimeInMillis(System.currentTimeMillis());
		//figure out each of my times, start with Core1_SleepCal
		String hour = Core1_SleepTime.getText().toString().split(":")[0];
		String minute = Core1_SleepTime.getText().toString().split(":")[1];
		Core1_SleepCal.add(Calendar.HOUR_OF_DAY, (Integer.parseInt(hour) - CurrentTime.get(Calendar.HOUR_OF_DAY)));
		Core1_SleepCal.add(Calendar.MINUTE, (Integer.parseInt(minute) - CurrentTime.get(Calendar.MINUTE)));
		Core1_SleepCal.set(Calendar.SECOND, 0);
		while(Core1_SleepCal.before(CurrentTime)) {
			Core1_SleepCal.add(Calendar.DAY_OF_MONTH, 1);
		}
		//Core1_WakeCal
		hour = Core1_WakeTime.getText().toString().split(":")[0];
		minute = Core1_WakeTime.getText().toString().split(":")[1];
		Core1_WakeCal.add(Calendar.HOUR_OF_DAY, (Integer.parseInt(hour) - CurrentTime.get(Calendar.HOUR_OF_DAY)));
		Core1_WakeCal.add(Calendar.MINUTE, (Integer.parseInt(minute) - CurrentTime.get(Calendar.MINUTE)));
		Core1_WakeCal.set(Calendar.SECOND, 0);
		while(Core1_WakeCal.before(CurrentTime)) {
			Core1_WakeCal.add(Calendar.DAY_OF_MONTH, 1);
		}
		
		//Nap1_SleepCal
		hour = Nap1_SleepTime.getText().toString().split(":")[0];
		minute = Nap1_SleepTime.getText().toString().split(":")[1];
		Nap1_SleepCal.add(Calendar.HOUR_OF_DAY, (Integer.parseInt(hour) - CurrentTime.get(Calendar.HOUR_OF_DAY)));
		Nap1_SleepCal.add(Calendar.MINUTE, (Integer.parseInt(minute) - CurrentTime.get(Calendar.MINUTE)));
		Nap1_SleepCal.set(Calendar.SECOND, 0);
		while(Nap1_SleepCal.before(CurrentTime)) {
			Nap1_SleepCal.add(Calendar.DAY_OF_MONTH, 1);
		}
		//Nap1_WakeCal
		hour = Nap1_WakeTime.getText().toString().split(":")[0];
		minute = Nap1_WakeTime.getText().toString().split(":")[1];
		Nap1_WakeCal.add(Calendar.HOUR_OF_DAY, (Integer.parseInt(hour) - CurrentTime.get(Calendar.HOUR_OF_DAY)));
		Nap1_WakeCal.add(Calendar.MINUTE, (Integer.parseInt(minute) - CurrentTime.get(Calendar.MINUTE)));
		Nap1_WakeCal.set(Calendar.SECOND, 0);
		while(Nap1_WakeCal.before(CurrentTime)) {
			Nap1_WakeCal.add(Calendar.DAY_OF_MONTH, 1);
		}
		
		//Nap2_SleepCal
		hour = Nap2_SleepTime.getText().toString().split(":")[0];
		minute = Nap2_SleepTime.getText().toString().split(":")[1];
		Nap2_SleepCal.add(Calendar.HOUR_OF_DAY, (Integer.parseInt(hour) - CurrentTime.get(Calendar.HOUR_OF_DAY)));
		Nap2_SleepCal.add(Calendar.MINUTE, (Integer.parseInt(minute) - CurrentTime.get(Calendar.MINUTE)));
		Nap2_SleepCal.set(Calendar.SECOND, 0);
		while(Nap2_SleepCal.before(CurrentTime)) {
			Nap2_SleepCal.add(Calendar.DAY_OF_MONTH, 1);
		}
		//Nap2_WakeCal
		hour = Nap2_WakeTime.getText().toString().split(":")[0];
		minute = Nap2_WakeTime.getText().toString().split(":")[1];
		Nap2_WakeCal.add(Calendar.HOUR_OF_DAY, (Integer.parseInt(hour) - CurrentTime.get(Calendar.HOUR_OF_DAY)));
		Nap2_WakeCal.add(Calendar.MINUTE, (Integer.parseInt(minute) - CurrentTime.get(Calendar.MINUTE)));
		Nap2_WakeCal.set(Calendar.SECOND, 0);
		while(Nap2_WakeCal.before(CurrentTime)) {
			Nap2_WakeCal.add(Calendar.DAY_OF_MONTH, 1);
		}
		
		//Nap3_SleepCal
		hour = Nap3_SleepTime.getText().toString().split(":")[0];
		minute = Nap3_SleepTime.getText().toString().split(":")[1];
		Nap3_SleepCal.add(Calendar.HOUR_OF_DAY, (Integer.parseInt(hour) - CurrentTime.get(Calendar.HOUR_OF_DAY)));
		Nap3_SleepCal.add(Calendar.MINUTE, (Integer.parseInt(minute) - CurrentTime.get(Calendar.MINUTE)));
		Nap3_SleepCal.set(Calendar.SECOND, 0);
		while(Nap3_SleepCal.before(CurrentTime)) {
			Nap3_SleepCal.add(Calendar.DAY_OF_MONTH, 1);
		}
		//Nap3_WakeCal
		hour = Nap3_WakeTime.getText().toString().split(":")[0];
		minute = Nap3_WakeTime.getText().toString().split(":")[1];
		Nap3_WakeCal.add(Calendar.HOUR_OF_DAY, (Integer.parseInt(hour) - CurrentTime.get(Calendar.HOUR_OF_DAY)));
		Nap3_WakeCal.add(Calendar.MINUTE, (Integer.parseInt(minute) - CurrentTime.get(Calendar.MINUTE)));
		Nap3_WakeCal.set(Calendar.SECOND, 0);
		while(Nap3_WakeCal.before(CurrentTime)) {
			Nap3_WakeCal.add(Calendar.DAY_OF_MONTH, 1);
		}
		//update the duration text
		long duration = (Core1_WakeCal.getTimeInMillis() - Core1_SleepCal.getTimeInMillis())/60000;
		if(duration<0){
			duration += 1440;
		}
		Core1_Duration.setText(Long.toString(duration) + " m");
		duration = (Nap1_WakeCal.getTimeInMillis() - Nap1_SleepCal.getTimeInMillis())/60000;
		if(duration<0){
			duration += 1440;
		}
		Nap1_Duration.setText(Long.toString(duration) + " m");
		duration = (Nap2_WakeCal.getTimeInMillis() - Nap2_SleepCal.getTimeInMillis())/60000;
		if(duration<0){
			duration += 1440;
		}
		Nap2_Duration.setText(Long.toString(duration) + " m");
		duration = (Nap3_WakeCal.getTimeInMillis() - Nap3_SleepCal.getTimeInMillis())/60000;
		if(duration<0){
			duration += 1440;
		}
		Nap3_Duration.setText(Long.toString(duration) + " m");
	}
}
